package com.google.code.sharecontent.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.google.code.sharecontent.dao.ResourceLogDao;
import com.google.code.sharecontent.model.ResourceLog;

public class ResourceLogDaoImpl  extends BaseDaoImpl<ResourceLog> implements ResourceLogDao {

	public ResourceLogDaoImpl(Connection conn) {
		super(conn);
	}

	@Override
	public ResourceLog createByRow(ResultSet rs) throws SQLException {
		ResourceLog resource = new ResourceLog();
		resource.setId(rs.getInt("ID"));
		resource.setResourceId(rs.getInt("RESOURCE_ID"));
		resource.setStatusId(rs.getInt("STATUS_ID"));
		resource.setUserId(rs.getInt("USER_ID"));
		resource.setAction(rs.getDate("ACTION_DATE"));
		return resource;
	}

	@Override
	protected String generateInsertSql(ResourceLog status) {
		return "insert into "+getTableName()+" (RESOURCE_ID, STATUS_ID, USER_ID) values (?, ?, ?)";
	}
	
	@Override
	protected String generateUpdateSql(ResourceLog status) {
		return "update "+getTableName()+" set RESOURCE_ID = ?," +
				"STATUS_ID = ?,USER_ID = ? "+
		" where ID = "+ status.getId();
	}

	@Override
	protected String getTableName() {
		return "resources_log";
	}

	@Override
	protected PreparedStatement putPrepareStatement(PreparedStatement pStat,
			ResourceLog status) throws SQLException {
		pStat.setInt(1, status.getResourceId());
		pStat.setInt(3, status.getStatusId());
		pStat.setInt(4, status.getUserId());
		return pStat;
	}

}
